<?php
if($_SERVER['REMOTE_ADDR'] == '127.0.0.1')
{
	$base_url = "http://www.iqtest.com/";
}
else 
{
	$base_url = "http://iqtest.msitrsu.com/";
}

function check_times_by_student_id($id_excercise,$student_id)
{
	include "dbconnect.php";
	$times = 0;
	$sqle = "SELECT times FROM student_excercise  WHERE id_student=".$student_id." AND id_excercise=".$id_excercise;
	$resulte = mysql_query($sqle, $link);
	mysql_query("SET NAMES UTF8");
	$numrows = mysql_num_rows($resulte);
	if($numrows > 0)
	{
		while ($arr = mysql_fetch_array($resulte))
		{
			$times =$arr['times'];
		}
	}

	return $times;
}


function get_info_excercise($id_excercise=0)
{
	include "../dbconnect.php";
	$data =array();
	if($id_excercise > 0)
	{
		$sql = "SELECT * FROM excercise WHERE id_excercise=".$id_excercise." AND status=1 LIMIT 1";
		$result = mysql_query($sql, $link);
		mysql_query("SET NAMES UTF8");
		$numrows = mysql_num_rows($result);
		if($numrows > 0)
		{
			while ($arr = mysql_fetch_array($result, MYSQL_ASSOC))
			{
				$data['title'] = $arr['title'];
				$data['limit_times'] = $arr['limit_times'];
				$data['id_group_rules'] = $arr['id_group_rules'];
			}
		}
	}
	return $data;
}

function get_ans_by_question_id($id_question=0)
{
	include "../dbconnect.php";
	$ans_num = 0;
	if($id_question > 0)
	{
		$sql = "SELECT count(id_answer) AS ans_num FROM answer WHERE id_question=".$id_question;
		$result = mysql_query($sql, $link);
		mysql_query("SET NAMES UTF8");
		$numrows = mysql_num_rows($result);
		if($numrows > 0)
		{
			while ($arr = mysql_fetch_array($result, MYSQL_ASSOC))
			{
				$ans_num = $arr['ans_num'];
			}
		}
	}
	return $ans_num;
}

function get_full_total($id_excercise=0)
{
	include "../dbconnect.php";
	$full_total = 0;
	if($id_excercise > 0)
	{
		$sql = "SELECT amount FROM excercise WHERE id_excercise=".$id_excercise;
		$result = mysql_query($sql, $link);
		mysql_query("SET NAMES UTF8");
		$numrows = mysql_num_rows($result);
		if($numrows > 0)
		{
			while ($arr = mysql_fetch_array($result, MYSQL_ASSOC))
			{
				$full_total = $arr['amount'];
			}
		}
	}
	return $full_total;
}

function get_result_detail($total_score=0,$id_group_rules=0)
{
	include "../dbconnect.php";
	$str = "";
	if($id_group_rules > 0)
	{
		$sql = "SELECT score,detail FROM rules WHERE id_group_rules=".$id_group_rules." ORDER BY sorting ASC";
		$result = mysql_query($sql, $link);
		mysql_query("SET NAMES UTF8");
		$numrows = mysql_num_rows($result);
		if($numrows > 0)
		{
			//$result_detail = $score = array();
			$i=0; 
			while ($arr = mysql_fetch_array($result, MYSQL_ASSOC))
			{
				if($total_score <= $arr['score'] && $i == 0)
				{
					$str =$arr['detail'];
					$i++;
				}				
			}
		}
	}
	return $str;
}

function get_score_by_excercise_and_student_id($student_id=0,$id_excercise=0,$times=0)
{
	include "../dbconnect.php";
	$score = "";
	if($student_id > 0 && $id_excercise > 0 && $times >0)
	{
		$sql = "SELECT * FROM student_excercise WHERE id_student =".$student_id." AND id_excercise=".$id_excercise." AND times=".$times;
		$result = mysql_query($sql, $link);
		mysql_query("SET NAMES UTF8");
		$numrows = mysql_num_rows($result);
		if($numrows > 0)
		{
			while ($arr = mysql_fetch_array($result, MYSQL_ASSOC))
			{
				$score = $arr['total_score']."/".$arr['full_score'];
			}
		}
	}
	return $score;
}

function checktimes_by_excercise_and_student_id($student_id=0,$id_excercise=0,$times=0)
{
	include "../dbconnect.php";
	$can =1;//can do
	if($student_id > 0 && $id_excercise > 0 && $times >0)
	{
		$sql = "SELECT times FROM student_excercise WHERE id_student =".$student_id." AND id_excercise=".$id_excercise." AND times=".$times;
		$result = mysql_query($sql, $link);
		mysql_query("SET NAMES UTF8");
		$numrows = mysql_num_rows($result);
		if($numrows > 0)
		{
			$can =0;
		}
	}
	return $can;
}

?>